package flexulous.util;

import flex2.tools.oem.Message; 
import flex2.tools.oem.Logger;
import org.gjt.sp.util.Log;
import errorlist.*;

import flexulous.FlexulousPlugin;

public class FlexulousLogger implements Logger
{
	private boolean useGrowl;
	private String growlLocation;
	
	public FlexulousLogger()
	{
		useGrowl = FlexulousPlugin.getModel().getGrowlIsValid();
		if(useGrowl){
			growlLocation = FlexulousPlugin.getModel().getGrowlLocation();
		}
	}
	
	
	public void log(Message msg, int errorCode, String source) { 
		Log.log(Log.WARNING, FlexulousLogger.class, "PATH:"+msg.getPath() + "LINE:"+msg.getLine()+" COL:"+msg.getColumn()+"ERROR:"+msg.getLevel());
		
		int level = 0;
		if(msg.getLevel() == Message.INFO){
			level = ErrorSource.WARNING;
		}else if(msg.getLevel() == Message.ERROR){
			level = ErrorSource.ERROR;
		}else if(msg.getLevel() == Message.WARNING){
			level = ErrorSource.WARNING;
		}
		try{
			if(useGrowl && msg.getLevel() == Message.ERROR){
				Runtime.getRuntime().exec(new String[]{"/usr/local/bin/growlnotify","-a", "jEdit", "-m",msg.getPath()+", Line:"+msg.getLine()+"\n"+msg.toString()});
			}
		}catch(Exception ex){
			
		}
		String path = "";
		if(msg.getPath() != null){
			path = msg.getPath();
		}
		int line = msg.getLine()-1;
		if(line < 0){
			line = 0;
		}
		
		int column = msg.getColumn()-1;
		if(column < 0){
			column = 0;
		}
		
		FlexulousPlugin.getErrorSource().addError(new DefaultErrorSource.DefaultError(FlexulousPlugin.getErrorSource(),level,path, line, column,column + 1,msg.toString()));
	}

}
